package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.Employee;
import com.taysear.parcel.domain.RolePermission;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class RolesFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	final TextField roleId = new TextField("Role id");
	final TextField rolename = new TextField("Rolename");
	OptionGroup rolesPermissions = new OptionGroup("Permissions");

	public RolesFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if ("roleId".equals(propertyId)) {
			roleId.setRequired(true);
			roleId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return roleId;
		} if ("rolename".equals(propertyId)) {
			rolename.setRequired(true);
			rolename.setWidth(Constants.COMMON_FIELD_WIDTH);
			rolename.setRequiredError("Rolename is required");
			rolename.setNullRepresentation("");
			return rolename;
		} if ("rolesPermissions".equals(propertyId)) {
			rolesPermissions.setMultiSelect(true);
			try{
				rolesPermissions = (OptionGroup) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<RolePermission> container = JPAContainerFactory.make(RolePermission.class, app.PERSISTENCE_UNIT);
				rolesPermissions.setContainerDataSource(container);
			}
			rolesPermissions.setWidth(Constants.COMMON_FIELD_WIDTH);
			rolesPermissions.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			return rolesPermissions;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
